package steak.xteamy.Network;

import de.maxhenkel.voicechat.api.Group;
import net.fabricmc.fabric.api.networking.v1.PayloadTypeRegistry;
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
import net.minecraft.entity.effect.StatusEffectInstance;
import net.minecraft.entity.effect.StatusEffects;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.network.codec.PacketCodec;
import net.minecraft.network.packet.CustomPayload;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.Text;
import steak.xteamy.KillerGroup;
import steak.xteamy.VoiceChat.GroupManager;

import java.util.UUID;

public class Packets {
    public static void registerPackets() {
        PayloadTypeRegistry.playS2C().register(Channel.ID, Channel.CODEC);
        PayloadTypeRegistry.playC2S().register(Channel.ID, Channel.CODEC);

        PayloadTypeRegistry.playC2S().register(HorizontalCollision.ID, HorizontalCollision.CODEC);
        networkHandler();
    }

    private static void networkHandler() {
        ServerPlayNetworking.registerGlobalReceiver(Channel.ID, (channel, context) -> {
            UUID uuid = context.player().getUuid();
            int channelP = channel.channelP();

            Group group = null;
            if (channelP == 1) {
                group = GroupManager.WOLF_GROUP;
            } else if (channelP == 2) {
                group = GroupManager.DEATH_GROUP;
            }
            GroupManager.update(uuid, group);
        });

        ServerPlayNetworking.registerGlobalReceiver(HorizontalCollision.ID, (payload, context) -> {
            ServerPlayerEntity player = context.player();
            boolean bool = payload.bool();

            if (player.hasStatusEffect(KillerGroup.HELICOPTER_MODE)) {
                if (bool) {
                    player.sendMessage(Text.of("§c哦不，你坠机了！"), true);
                    player.removeStatusEffect(KillerGroup.HELICOPTER_MODE);
                    player.addStatusEffect(new StatusEffectInstance(StatusEffects.WEAKNESS, 3600, 0, false, false));
                }
            }
        });
    }

    public record Channel(int channelP) implements CustomPayload {

        public static final Id<Channel> ID = new CustomPayload.Id<>(KillerGroup.IdentifierMod("channel"));
        public static final PacketCodec<PacketByteBuf, Channel> CODEC = PacketCodec.of(Channel::write, Channel::new);

        public Channel(PacketByteBuf buf) {
            this(buf.readInt());
        }

        private void write (PacketByteBuf buf) {
            buf.writeInt(this.channelP);
        }

        @Override
        public Id<? extends CustomPayload> getId() {
            return ID;
        }
    }

    public record HorizontalCollision(boolean bool) implements CustomPayload {

        public static final Id<HorizontalCollision> ID = new CustomPayload.Id<>(KillerGroup.IdentifierMod("horizontal_collision"));
        public static final PacketCodec<PacketByteBuf, HorizontalCollision> CODEC = PacketCodec.of(HorizontalCollision::write, HorizontalCollision::new);

        public HorizontalCollision(PacketByteBuf buf) {
            this(buf.readBoolean());
        }

        private void write (PacketByteBuf buf) {
            buf.writeBoolean(this.bool);
        }

        @Override
        public Id<? extends CustomPayload> getId() {
            return ID;
        }
    }
}
